home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / PCI Driver Development Kit / • Tools / Utility / LogLibrary 950622 / Src / StatusWindow.c < prev   
Encoding:
C/C++ Source or Header  |  1996-08-20  |  7.1 KB  |  266 lines  |  [TEXT/MPCC]

  1. /*                                    StatusWindow.c                                    */
  2.  
  3. #include <Errors.h>
  4. #include <Script.h>
  5. #include <Types.h>
  6. #include <Files.h>
  7. #include <Resources.h>
  8. #include <QuickDraw.h>
  9. #include <Fonts.h>
  10. #include <Events.h>
  11. #include <Windows.h>
  12. #include <ToolUtils.h>
  13. #include <Memory.h>
  14. #include <Menus.h>
  15. #include <Lists.h>
  16. #include <Printing.h>
  17. #include <Dialogs.h>
  18. #include <StandardFile.h>
  19.  
  20. #ifndef TESTING            /* Defined in the TestStatusWindow project prefix dialog    */
  21. #define TESTING    0
  22. #endif
  23.  
  24. #ifndef FALSE
  25. #define FALSE    0
  26. #define TRUE    1
  27. #endif
  28.  
  29. WindowPtr                    MakeStatusWindow(void);
  30. void                        UpdateStatusWindow(
  31.         WindowPtr                statusWindowPtr
  32.     );
  33. void                        ShowStatusString(
  34.         WindowPtr                statusWindowPtr,
  35.         ConstStr255Param        theText
  36.     );
  37.  
  38. /*
  39.  * Very simple logging window. The configuration data are burned in, but could
  40.  * easily be passed as parameters.
  41.  */
  42. #define COLS        80
  43. #define FONT_NUMBER    (applFont)
  44. #define FONT_SIZE    (9)
  45. #define width(r)                ((r).right - (r).left)
  46. #define height(r)                ((r).bottom - (r).top)
  47.  
  48. typedef struct StatusWindowRecord {
  49.     WindowRecord            windowRecord;        /* The status window            */
  50.     Rect                    topLineRect;        /* Line[0] display rect            */
  51.     Rect                    lineRect;            /* Current line display rect    */
  52.     unsigned short            lastUnderline;        /* Previous line @ leading        */
  53.     unsigned short            currentLine;        /* Current line number            */
  54.     unsigned short            nLines;                /* Number of lines in window    */
  55.     unsigned short            lineHeight;            /* ascent + descent + leading    */
  56.     unsigned short            lineAscent;            /* Just the font ascent            */
  57.     unsigned short            underlineOffset;    /* Where to draw the underline    */
  58.     Ptr                        textArea;            /* All text goes here            */
  59.     StringPtr                textStrings[1];        /* -> each text line            */
  60. } StatusWindowRecord, *StatusWindowRecordPtr;
  61. #define STATUS        (*((StatusWindowRecordPtr) statusWindowPtr))
  62. #define WINDOW         (*((WindowPtr) &STATUS.windowRecord))
  63.  
  64. WindowPtr
  65. MakeStatusWindow(void)
  66. {
  67.         Rect                    windowRect;
  68.         StatusWindowRecordPtr    statusWindowPtr;
  69.         WindowPtr                theWindow;
  70.         FontInfo                info;
  71.         short                    lineHeight;
  72.         short                    nLines;
  73.         GrafPtr                    savePort;
  74.         GrafPort                tempPort;
  75.         Size                    statusRecordSize;
  76.         short                    i;
  77.         
  78.         GetPort(&savePort);
  79.         /*
  80.          * Create a temporary port so we can get the line height before
  81.          * creating the window... and without messing up the current port.
  82.          */
  83.         OpenPort(&tempPort);        /* Does a SetPort    */
  84.         TextFont(FONT_NUMBER);
  85.         TextSize(FONT_SIZE);
  86.         GetFontInfo(&info);
  87.         ClosePort(&tempPort);
  88.         SetPort(savePort);
  89.         lineHeight = info.ascent + info.descent + info.leading;
  90.         /*
  91.          * The window occupies the right-half of the display. This, too, could
  92.          * be passed in from the caller.
  93.          */
  94.         windowRect = qd.screenBits.bounds;
  95.         InsetRect(&windowRect, 4, 4);
  96.         windowRect.top += (GetMBarHeight() * 2);
  97.         windowRect.left = windowRect.right - (width(windowRect) / 2);
  98.         nLines = (height(windowRect) - 2) / lineHeight;
  99.         windowRect.top = windowRect.bottom - ((nLines * lineHeight) + 2);
  100.         /* */
  101.         statusRecordSize = sizeof (StatusWindowRecord)    /* Constant info        */
  102.                     - sizeof (StringPtr)                /* Remove C array hack    */
  103.                     + (nLines * sizeof (StringPtr));    /* Add in string vector    */
  104.         statusWindowPtr = (StatusWindowRecordPtr)
  105.                     NewPtrClear(statusRecordSize);
  106.         if (statusWindowPtr != NULL) {
  107.             STATUS.textArea = NewPtr(nLines * (COLS + 1));    /* String data area    */
  108.             if (STATUS.textArea == NULL) {
  109.                 DisposePtr((Ptr) statusWindowPtr);
  110.                 statusWindowPtr = NULL;
  111.             }
  112.         }
  113.         if (statusWindowPtr != NULL) {
  114.             theWindow = NewCWindow(
  115.                         &STATUS.windowRecord,    /* Our storage                    */
  116.                         &windowRect,            /* Dummy window shape            */
  117.                         "\pLog Display",        /* Window title                    */
  118.                         FALSE,                    /* Invisible                    */
  119.                         noGrowDocProc,            /* Minimal window                */
  120.                         (WindowPtr) -1L,        /* In front                        */
  121.                         TRUE,                    /* Has close box                */
  122.                         0                        /* RefCon                        */
  123.                     );
  124.             if (theWindow == NULL) {
  125.                 DisposePtr(STATUS.textArea);
  126.                 DisposePtr((Ptr) statusWindowPtr);
  127.                 statusWindowPtr = NULL;
  128.             }
  129.         }
  130.         if (statusWindowPtr != NULL) {
  131.             SetPort(theWindow);
  132.             TextFont(FONT_NUMBER);
  133.             TextSize(FONT_SIZE);
  134.             GetFontInfo(&info);
  135.             STATUS.currentLine = 0;
  136.             STATUS.lastUnderline = 0;
  137.             STATUS.lineAscent = info.ascent;
  138.             STATUS.lineHeight = info.ascent + info.descent + info.leading;
  139.             STATUS.underlineOffset = info.ascent + info.descent;
  140.             STATUS.nLines = nLines;
  141.             STATUS.lineRect = WINDOW.portRect;
  142.             InsetRect(&STATUS.lineRect, 1, 1);
  143.             STATUS.lineRect.bottom = STATUS.lineRect.top + STATUS.lineHeight;
  144.             STATUS.topLineRect = STATUS.lineRect;
  145.             /*
  146.              * Initialize the vector of string pointers to point to the string area.
  147.              */
  148.             for (i = 0; i < nLines; i++) {
  149.                 STATUS.textStrings[i] = (StringPtr) &STATUS.textArea[i * (COLS + 1)];
  150.                 STATUS.textStrings[i][0] = 0;    /* Initially "\p"    */
  151.             }
  152.             ShowWindow(theWindow);
  153.         }
  154.         SetPort(savePort);
  155.         return ((WindowPtr) statusWindowPtr);
  156. }
  157.  
  158. void
  159. UpdateStatusWindow(
  160.         WindowPtr                statusWindowPtr
  161.     )
  162. {
  163.         short                    i;
  164.         GrafPtr                    savePort;
  165.         Point                    viewPoint;
  166.         
  167.         GetPort(&savePort);
  168.         SetPort(statusWindowPtr);        
  169.         TextFont(FONT_NUMBER);
  170.         TextSize(FONT_SIZE);
  171.         viewPoint.h = STATUS.topLineRect.left;
  172.         viewPoint.v = (STATUS.topLineRect.top + STATUS.lineAscent);
  173.         for (i = 0; i < STATUS.nLines; i++) {
  174.             if (STATUS.textStrings[i][0] != 0) {
  175.                 MoveTo(viewPoint.h, viewPoint.v);
  176.                 DrawString(STATUS.textStrings[i]);
  177.             }
  178.             viewPoint.v += STATUS.lineHeight;
  179.         }
  180.         if (STATUS.lastUnderline != 0) {
  181.             MoveTo(WINDOW.portRect.left, STATUS.lastUnderline);
  182.             LineTo(WINDOW.portRect.right, STATUS.lastUnderline);
  183.         }
  184.         SetPort(savePort);
  185. }
  186.  
  187. void
  188. ShowStatusString(
  189.         WindowPtr                statusWindowPtr,
  190.         ConstStr255Param        theText
  191.     )
  192. {
  193.         short                    textSize;
  194.         GrafPtr                    savePort;
  195.         StringPtr                thisLine;
  196.         Rect                    viewRect;
  197.         
  198.         GetPort(&savePort);
  199.         SetPort(statusWindowPtr);
  200.         textSize = theText[0];
  201.         if (textSize > COLS)
  202.             textSize = COLS;
  203.         thisLine = STATUS.textStrings[STATUS.currentLine];
  204.         BlockMove(&theText[1], &thisLine[1], textSize);
  205.         thisLine[0] = textSize;
  206.         if (theText[0] > COLS)
  207.             thisLine[COLS - 1] = '…';
  208.         if (STATUS.lastUnderline != 0) {
  209.             SetRect(
  210.                 &viewRect,
  211.                 WINDOW.portRect.left,
  212.                 STATUS.lastUnderline,
  213.                 WINDOW.portRect.right,
  214.                 STATUS.lastUnderline + 1
  215.             );
  216.             EraseRect(&viewRect);
  217.         }
  218.         EraseRect(&STATUS.lineRect);
  219.         MoveTo(
  220.             STATUS.lineRect.left,
  221.             STATUS.lineRect.top + STATUS.lineAscent
  222.         );
  223.         DrawString(thisLine);
  224.         STATUS.lastUnderline = STATUS.lineRect.top + STATUS.underlineOffset;
  225.         MoveTo(STATUS.lineRect.left, STATUS.lastUnderline);
  226.         LineTo(STATUS.lineRect.right, STATUS.lastUnderline);
  227.         if (++STATUS.currentLine < STATUS.nLines)
  228.             OffsetRect(&STATUS.lineRect, 0, STATUS.lineHeight);
  229.         else {
  230.             STATUS.currentLine = 0;
  231.             STATUS.lineRect = STATUS.topLineRect;
  232.         }
  233.         SetPort(savePort);
  234. }
  235.  
  236. #if TESTING
  237.  
  238. WindowPtr        gStatusWindow;
  239.  
  240. void
  241. main(void)
  242. {
  243.         MaxApplZone();        
  244.         InitGraf(&qd.thePort);
  245.         InitFonts();
  246.         InitWindows();
  247.         InitMenus();
  248.         TEInit();
  249.         InitDialogs(0);
  250.         /* */
  251.         gStatusWindow = MakeStatusWindow();
  252.         if (gStatusWindow == NULL)
  253.             DebugStr("\pDisplay window failed");
  254.         else {
  255.             int                        i;
  256.             
  257.             for (i = 0; i < 40; i++)
  258.                 ShowStatusString(gStatusWindow, "\pHello world!");
  259.             while (Button() == FALSE)
  260.                 ;
  261.             while (Button() != FALSE)
  262.                 ;
  263.         }
  264. }
  265. #endif
  266.